// 用于存放频道相关的数据
import { getChannelList, getAllChannelList } from '@/api/channel'
export default {
  namespaced: true,
  state: {
    channelList: [],
    allChannelList: []
  },
  mutations: {
    setChannelList (state, payLoad) {
      state.channelList = payLoad
    },
    setAllChannelList (state, payload) {
      state.allChannelList = payload
    },
    addChannel (state, channel) {
      state.channelList.push(channel)
    }
  },
  actions: {
    async getChannelList (context) {
      const res = await getChannelList()
      // console.log(res.data.channels)
      context.commit('setChannelList', res.data.channels)
    },
    async getAllChannelList (context) {
      const res = await getAllChannelList()
      context.commit('setAllChannelList', res.data.channels)
    }
  },
  getters: {
    // 可选的频道
    optionalChannels (state) {
      return state.allChannelList.filter(item => state.channelList.findIndex(v => v.id === item.id) === -1)
    }
    // optionalChannels: state => {
    //   // 对 allChannels 中的元素进行过滤, 只保留那些没有在 channels 中出现的元素
    //   return state.allChannels.filter(channel => {
    //     return state.channels.filterIndex(item => item.id === channel.id) === -1
    //   })
    // }
  }
}
